﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Web.Security;
using System.Resources;
namespace WebTeamplate
{

    public partial class Admin_PhieuChuyenKho : BasePage
    {

        #region Fields
        public string form = string.Empty;
        public string urlList = "DMPhieuChuyenKho.aspx";
        private int indexGenControl = 0;
        #endregion

        #region ViewStates
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }


        public Int64 MaPhieuChuyen
        {

            get
            {
                object o = ViewState["MaPhieuChuyen"];
                if (o != null) return (Int64)o;
                return Int64.MinValue;
            }

            set { ViewState["MaPhieuChuyen"] = value; }

        }
        public string userNameLogin
        {

            get
            {
                object o = ViewState["userNameLogin"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["userNameLogin"] = value; }

        }
        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(WebTeamplate.Admin_PhieuChuyenKho));
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
            if (!Page.IsPostBack)
            {
                InitParamater();
                InitTextControl();
                CommonFunction.LoadSubCate(drlKhoFrom, CommonFunction.DEFAULT_WAREHOUSE_CODE, true);
                if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
                    CommonFunction.LoadSubCate(drlKhoTo, CommonFunction.DEFAULT_WAREHOUSE_CODE, true);
                CommonFunction.LoadSubCate(drlChungXe, CommonFunction.DEFAULT_CHUNGLOAIXE_CODE, true);
                if (this.Command == CommonFunction.DEFAULT_COMMAND_EDIT)
                {
                    LoadData();
                    GenarateListFormUpdate();
                    trAddRowControl.Visible = true;
                    InitAutoCompleteData();
                }
            }
        }
        [Ajax.AjaxMethod()]
        public string GeMatHangInforByMaPT(string maPT, string kho, string row)
        {
            int dvt = 0;
            MatHang objMatHang = MatHangUtil.GetMatHangByMaPhuTungInPhieuNhaphang(maPT);
            if (objMatHang != null)
            {
                string optionDVT = string.Empty;
                string dvtControl = string.Empty;
                //get DVT
                List<SubCategoryResult> lstDVT = SubCategoryUtil.GetDonViTinhByMatHang(objMatHang.MaMatHang);
                if (lstDVT != null)
                {

                    optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                    foreach (SubCategoryResult objDVT in lstDVT)
                    {
                        if (objDVT.SubCategoryId == objMatHang.DVT)
                        {
                            dvt = objDVT.SubCategoryId;
                            optionDVT += "<option selected='selected'  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                        }
                        else
                        {
                            optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";

                        }
                    }

                    dvtControl = @"<select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange='CheckSoLuongTonMatHang(" + row + ",this.value)' id='drlDonViTinh_" + row + @"'  style='width:98%'>  " + optionDVT + @"                             
                            </select>";

                }
                int result = MatHangUtil.GetSoLuongTonByDonViTinh(objMatHang.MaMatHang, ConvertValueObject.ParseInt(kho), dvt);

                string xe = string.Empty;
                DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
                if (objXe != null)
                    xe = objXe.TenXe;
                return objMatHang.MaMatHang + "*+*" + objMatHang.MaPhuTung + "*+*" + xe + "*+*" + objMatHang.TenNuocSX + "*+*" + objMatHang.Hieu + "*+*" + objMatHang.TenMatHang + "_" + objMatHang.MaMatHang + "*+*" + dvtControl + "*+*" + dvt + "*+*" + result;
            }
            else return "*+*";
        }

        [Ajax.AjaxMethod()]
        public string GeMatHangInfor(string maMatHang, string kho, string row)
        {
            int dvt = 0;
            MatHang objMatHang = MatHangUtil.Get(maMatHang);
            if (objMatHang != null)
            {
             
                string optionDVT = string.Empty;
                string dvtControl = string.Empty;
                string nuocSX = string.Empty;
                
                SubCategory objCountry = SubCategoryUtil.Get(objMatHang.MaNuocSanXuat, CommonFunction.DEFAULT_COUNTRY_CODE);
                if (objCountry != null)
                    nuocSX = objCountry.SubCategoryName;

                //get DVT
                List<SubCategoryResult> lstDVT = SubCategoryUtil.GetDonViTinhByMatHang(objMatHang.MaMatHang);
                if (lstDVT != null)
                {

                    optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                    foreach (SubCategoryResult objDVT in lstDVT)
                    {
                        if (objDVT.SubCategoryId == objMatHang.DVT)
                        {
                            dvt = objDVT.SubCategoryId;
                            optionDVT += "<option selected='selected'  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                        }
                        else
                        {
                            optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";

                        }
                    }

                    dvtControl = @"<select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange='CheckSoLuongTonMatHang(" + row + ",this.value)' id='drlDonViTinh_" + row + @"'  style='width:98%'>  " + optionDVT + @"                             
                            </select>";

                }
                int result = MatHangUtil.GetSoLuongTonByDonViTinh(objMatHang.MaMatHang, ConvertValueObject.ParseInt(kho), dvt);
                string xe = string.Empty;
                DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
                if (objXe != null)
                    xe = objXe.TenXe;
                return maMatHang + "*+*" + objMatHang.MaPhuTung + "*+*" + xe + "*+*" + nuocSX + "*+*" + objMatHang.Hieu + "*+*" + dvtControl + "*+*" + dvt + "*+*" + result;
            }
            else return "*+*";
        }
        
        private void InitAutoCompleteData()
        {
            List<ChiTietTonKho> lstMH = MatHangTonKhoUtil.GetThongKeTonKho(ConvertValueObject.ParseInt(drlKhoFrom.SelectedValue), int.MinValue, int.MinValue, int.MinValue, string.Empty);
            string item = string.Empty;
            for (int i = 0; i < lstMH.Count; i++)
            {
                ChiTietTonKho objMH = lstMH[i];
                if (objMH.Type != 1)
                {
                    if (i == lstMH.Count - 1)
                        item += "'" + objMH.Name + "_" + objMH.Id + "'";
                    else item += "'" + objMH.Name + "_" + objMH.Id + "',";
                }
            }
            string scriptATCL = @"<script>
            var customarray = new Array(" + item + @");            
            </script>";
            item = string.Empty;
            indexGenControl = int.Parse(hidTotalRow.Value.ToString());
            for (int i = 1; i <= indexGenControl; i++)
            {
                item += "var obj" + i + " = actb(document.getElementById('txtTenMH_" + i + "'), customarray); ";
            }
            scriptATCL += @" <script>
           " + item + @"           
        </script>";
            ltrScript.Text = scriptATCL;
        }
        List<ChiTietPhieuChuyenKhoResult> lstChiTiet = new List<ChiTietPhieuChuyenKhoResult>();
        private void GenarateListFormUpdate()
        {
            lstChiTiet = PhieuChuyenKhoUtil.GetByMaPhieuChuyenKho(this.MaPhieuChuyen);
            int maxRowGenerate = 0;
            int maxRowShow = 0;
            if (lstChiTiet.Count > CommonFunction.PHIEUCHUYENKHO_MAXROW_GENNARATE)
            {
                maxRowGenerate = lstChiTiet.Count;
                maxRowShow = maxRowGenerate;
            }
            else
            {
                maxRowGenerate = CommonFunction.PHIEUCHUYENKHO_MAXROW_GENNARATE;
                maxRowShow = lstChiTiet.Count;
            }
            hidTotalRow.Value = maxRowGenerate.ToString();
            hidMaxRowShow.Value = maxRowShow.ToString();
            hidCurrentRowShow.Value = maxRowShow.ToString();
            string result = GenerateHeader();            
            for (int i = 1; i <= maxRowGenerate; i++)
            {                
                if (i <= maxRowShow)
                    result += GenarateRowUpdate(i,"");
                else result += GenarateRowUpdate(i,"none");

            }
            result += @"</table>";
            form = result;
            indexGenControl = maxRowGenerate;
        }
        private string GenarateRowUpdate(int indexRow, string display)
        {

            ChiTietPhieuChuyenKhoResult objData = null;
            try
            {
                objData = lstChiTiet[indexRow - 1];
            }
            catch
            { }
            string result = string.Empty;
            if (objData == null)
            {

                string option = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                result = @"<tr style='display:" + display + "' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>                        
                         <td id='td_" + indexRow + @"'>
                             <input type='text' value='' id='txtTenMH_" + indexRow + @"' autocomplete='off' onblur='GetMHByname(this.value," + indexRow + @");'  style='width:" + (wTenMH - 10) + @"px;' class='detailedViewTextBoxList1 ctrListForm' />                            
                        </td>                    
                        <td>
                            <input type='text' value='' id='txtMaMH_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' />
                        </td>
                        <td>
                            <input type='text' value='' id='txtMaPT_" + indexRow + @"' onblur='GetMHByMaPT(this.value," + indexRow + @")'  style='width:98%' class='detailedViewTextBoxList ctrListForm ' />
                        </td>
                        <td>
                            <input type='text' value='' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm ' />
                        </td>

                        <td>
                             <input type='text' value='' id='txtNuocSX_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList1_Disable ctrListForm'
                                onfocus='TextBoxLeftFocus(this);' onblur='TextBoxLeftBlur(this);' />
                        </td>
                        <td>
                            <input type='text' value='' id='txtHieu_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm'
                                onfocus='TextBoxLeftFocus(this);' onblur='TextBoxLeftBlur(this);' />
                        </td>
                        
                        <td id='tdDVT_" + indexRow + @"'>
                            <select class='listForm ctrListForm' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>
                               " + hidOptionDVT.Value + @"
                            </select>
                        </td>    
                    
                        <td class='repeaterRightData'>
                               <input type='text'  class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' style='width: 100%;' disabled='disabled' id='txtSoLuongTon_" + indexRow + @"' value=''>
                            </td>                    
                        <td class='repeaterRightData'>
                                <input type='text' onblur=CheckSoLuongChuyen(this.value,'" + indexRow + @"'); class='detailedViewTextBoxListOn1 txtNumber' style='width: 100%;' id='txtSoLuongChuyen_" + indexRow + @"' value=''>
                            </td>
                        

                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a>
                        </td>
                    </tr>";
                return result;
            }
            else
            {
                MatHang objMatHang = MatHangUtil.Get(objData.MaMatHang);
                if (objMatHang == null)
                    return string.Empty;

                List<SubCategoryResult> lstDVT = SubCategoryUtil.GetDonViTinhByMatHang(objMatHang.MaMatHang);

                string optionDVT = string.Empty;
                optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                foreach (SubCategoryResult objDVT in lstDVT)
                {
                    if (objDVT.SubCategoryId == objData.MaDonViTinh)
                        optionDVT += "<option selected='selected' value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                    else optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                }
                //selected="selected
                //hidOptionDVT.Value = optionDVT;

                DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
                string xe = string.Empty;
                string nuocSX = string.Empty;
                if (objXe != null)
                {
                    drlChungXe.SelectedValue = objXe.ChungXe.ToString();
                    xe = objXe.TenXe;
                }
                SubCategory objCountry = SubCategoryUtil.Get(objMatHang.MaNuocSanXuat, CommonFunction.DEFAULT_COUNTRY_CODE);
                if (objCountry != null)
                    nuocSX = objCountry.SubCategoryName;

                int soLuongTon = MatHangUtil.GetSoLuongTonByDonViTinh(objMatHang.MaMatHang, ConvertValueObject.ParseInt(drlKhoFrom.SelectedValue), objData.MaDonViTinh);
               // string strOptionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                result = @"<tr style='display:" + display + @";' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>
                       
                        
                        
                        <td id='td_" + indexRow + @"'>
                             <input type='text' value='" + objMatHang.TenMatHang + "_" + objMatHang.MaMatHang + @"' id='txtTenMH_" + indexRow + @"' autocomplete='off' onblur='GetMHByname(this.value," + indexRow + @");' disabled='disabled'  style='width:" + (wTenMH - 10) + @"px;' class='detailedViewTextBoxList_Disable1 ctrListForm' />
                            
                        </td>
                        <td>
                            <input type='text' value='" + objData.MaMatHang + "' id='txtMaMH_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' />
                        </td>
                    " + CreateTDMaPTUpdate(objMatHang.MaPhuTung, indexRow,true) + @"
<td>
                            <input type='text' value='" + xe + @"' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm ' />
                        </td>
                        <td>
                             <input type='text' value='" + nuocSX + "' id='txtNuocSX_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm'/>
                        </td>
                        <td>
                            <input type='text' value='" + objMatHang.Hieu + "' id='txtHieu_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm' />
                        </td>
                       
                        <td id='tdDVT_" + indexRow + @"'>
                            <select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange='CheckSoLuongTonMatHang(" + indexRow + ",this.value)' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>
                               " + optionDVT + @"
                            </select>
                        </td>
                    <td class='repeaterRightData'>
                               <input type='text'  class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' style='width: 100%;' disabled='disabled' id='txtSoLuongTon_" + indexRow + @"' value='" + soLuongTon + @"'>
                            </td>
                            <td class='repeaterRightData'>
                                <input type='text' onblur=CheckSoLuongChuyen(this.value,'" + indexRow + @"'); class='detailedViewTextBoxListOn1 txtNumber' style='width: 100%;' id='txtSoLuongChuyen_" + indexRow + @"' value='" + objData.SoLuongChuyen + @"'>
                            </td>

                       
                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a>
                        </td>
                    </tr>";
                return result;
            }
            return string.Empty;
        }
        private string CreateTDMaPTUpdate(string maPT, int indexRow,bool enable)
        {
            if(enable || maPT==string.Empty)
                return @"<td>
                            <input type='text' value='" + maPT + @"' onblur='GetMHByMaPT(this.value," + indexRow + @")' id='txtMaPT_" + indexRow + @"' disabled='true'  style='width:98%' class='detailedViewTextBoxList_Disable ctrListForm' />
                        </td>";
            else
            
                return @"<td>
                            <input type='text' value='" + maPT + @"' onblur='GetMHByMaPT(this.value," + indexRow + @")' id='txtMaPT_" + indexRow + @"' disabled='false'  style='width:98%' class='detailedViewTextBoxList ctrListForm ' />
                        </td>";
        }
        private void LoadData()
        {
            PhieuChuyenKho objPhieuChuyen = PhieuChuyenKhoUtil.Get(this.MaPhieuChuyen);
            if (objPhieuChuyen != null)
            {
                drlKhoFrom.SelectedValue = objPhieuChuyen.MaKhoChuyen.ToString();
                //CommonFunction.LoadSubCate(drlKhoTo, CommonFunction.DEFAULT_WAREHOUSE_CODE, true);
                LoadDMKho();
                drlKhoTo.SelectedValue = objPhieuChuyen.MaKhoDen.ToString();
                drlChungXe.SelectedValue = objPhieuChuyen.ChungXe.ToString();
                drlKhoFrom.Enabled = false;
                txtNgayNhap.Text = ConvertValueObject.ParseString(objPhieuChuyen.NgayNhap);
            }
            else Response.Redirect("DMPhieuChuyenKho.aspx");
        }    
        
        #endregion

        #region Methos

        private void InitParamater()
        {
            this.MaPhieuChuyen = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "id"));
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
        }
        private void LoadDMKho()
        {
            List<SubCategoryResult> lstCate = SubCategoryUtil.GetAllByCategory(CommonFunction.DEFAULT_WAREHOUSE_CODE);
            drlKhoTo.Items.Clear();
            ListItem nullItem = new ListItem(BaseFunctions.COMBOBOX_NULL_VALUE, string.Empty);
            drlKhoTo.Items.Add(nullItem);
            foreach (SubCategoryResult objCate in lstCate)
            {
                if (objCate.SubCategoryId.ToString() != drlKhoFrom.SelectedValue.ToString())
                    drlKhoTo.Items.Add(new ListItem(objCate.SubCategoryName, objCate.SubCategoryId.ToString()));
            }

        }
        private void InitTextControl()
        {
            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCacel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            UserProfile user = UserProfileUtil.Get(this.applicationName, this.userloginId);
            if (user != null)
                txtNguoiNhap.Text = user.Name;
            this.userNameLogin = this.userLoginName;
            txtNgayNhap.Text = ConvertValueObject.ParseString(CommonFunction.Now);
        }
        protected void btnCacel_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlList);
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.GetCurrentPageAndParamaterUrl());
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
        }
        protected void drlKhoFrom_SelectedIndexChanged(object sender, EventArgs e)
        {

            LoadDMKho();
            GenarateFormInsert();
            InitAutoCompleteData();
            trAddRowControl.Visible = true;
        }
        protected void drlChungXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            GenarateFormInsert();
        }
        int wChungXe = 100;
        int wTenMH = 320;
        int wMaMH = 100;
        int wSoLuongTon = 80;
        int wXe = 100;
        int wMaPhuTung = 180;
        int wHieu = 90;
        int wNuocSX = 100;
        int wSoLuong = 80;
        int wDVT = 70;
        int wGia = 170;
        int WDel = 40;
        int WGiaban = 40;
        int heightCtr = 26;
        public string GenerateHeader()
        {
            string result = @"
                <table width='100%' cellspacing='1' cellpadding='3' border='0' class='lvt small'>
                        
                            <tr>
                                
                                
                                <td class='lvtCol' style='width: " + wTenMH + @"px;'>
                                   Tên MH
                                </td>
<td class='lvtCol' style='width: " + wMaMH + @"px;'>
                                  Mã MH
                                </td>
                                <td class='lvtCol' style='width: " + wMaPhuTung + @"px;'>
                                   Mã phụ tùng
                                </td>
                                <td class='lvtCol' style='width: " + wXe + @"px;'>
                                   Xe
                                </td>
                                <td class='lvtCol' style='width: " + wNuocSX + @"px;'>
                                   Nước SX
                                </td>
                                 <td class='lvtCol' style='width: " + wHieu + @"px;'>
                                   Hiệu
                                </td>
                                <td class='lvtCol' style='width: " + wDVT + @"px;'>
                                   Đơn vị tính
                                </td>
                                <td class='lvtCol' style='width: " + wSoLuongTon + @"px;'>
                                    Số lượng tồn
                                </td>
                                <td class='lvtCol' style='width: " + wSoLuong + @"px;'>
                                    Số lượng chuyển
                                </td>
                                <td class='lvtCol' style='width:40px;'>
                                    &nbsp;
                                </td>
                               
                            </tr>";

            return result;
        }
        private void GenarateFormInsert()
        {
            hidMaMH.Value = string.Empty;

            List<ChiTietTonKho> lstResult = MatHangTonKhoUtil.GetThongKeTonKho(ConvertValueObject.ParseInt(drlKhoFrom.SelectedValue), int.MinValue, ConvertValueObject.ParseInt(drlChungXe.SelectedValue), int.MinValue, string.Empty);
            if (lstResult != null && lstResult.Count > 0)
            {
                int maxRowGenerate = CommonFunction.PHIEUCHUYENKHO_MAXROW_GENNARATE;
                int maxRowShow = CommonFunction.PHIEUCHUYENKHO_MAXROW_SHOW;
                if (maxRowGenerate > lstResult.Count)
                    maxRowGenerate = lstResult.Count;

                if (maxRowShow > maxRowGenerate)
                    maxRowShow = maxRowGenerate;

                hidTotalRow.Value = maxRowGenerate.ToString();
                hidMaxRowShow.Value = maxRowShow.ToString();
                hidCurrentRowShow.Value = maxRowShow.ToString();

                indexGenControl = maxRowGenerate;
                string result = GenerateHeader();
                for (int i = 1; i <= maxRowGenerate; i++)
                {
                    if (i <= maxRowShow)
                        result += GenarateRowInsert(i, "");
                    else result += GenarateRowInsert(i, "none");
                }
                result += @"</table>";
                indexGenControl = maxRowGenerate;
                form = result;
            }

        }
        private string GenarateRowInsert(int indexRow, string display)
        {

            string strOptionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            string result = @"<tr style='display:" + display + @";' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>
                       
                        
                        
                        <td id='td_" + indexRow + @"'>
                             <input type='text' value='' id='txtTenMH_" + indexRow + @"' autocomplete='off' onblur='GetMHByname(this.value," + indexRow + @");'  style='width:" + (wTenMH - 10) + @"px;' class='detailedViewTextBoxList1 ctrListForm' />
                            
                        </td>
                        <td>
                            <input type='text' value='' id='txtMaMH_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' />
                        </td>
                    <td>
                            <input type='text' value='' id='txtMaPT_" + indexRow + @"' onblur='GetMHByMaPT(this.value," + indexRow + @")'  style='width:98%' class='detailedViewTextBoxList ctrListForm ' />
                        </td>
<td>
                            <input type='text' value='' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm ' />
                        </td>
                        <td>
                             <input type='text' value='' id='txtNuocSX_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm'/>
                        </td>
                        <td>
                            <input type='text' value='' id='txtHieu_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm' />
                        </td>
                       
                        <td id='tdDVT_" + indexRow + @"'>
                            <select class='listForm ctrListForm' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>
                               " + strOptionDVT + @"
                            </select>
                        </td>
                    <td class='repeaterRightData'>
                               <input type='text'  class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber' style='width: 100%;' disabled='disabled' id='txtSoLuongTon_" + indexRow + @"' value=''>
                            </td>
                            <td class='repeaterRightData'>
                                <input type='text' onblur=CheckSoLuongChuyen(this.value,'" + indexRow + @"'); class='detailedViewTextBoxListOn1 txtNumber' style='width: 100%;' id='txtSoLuongChuyen_" + indexRow + @"' value=''>
                            </td>

                       
                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a>
                        </td>
                    </tr>";
            return result;
        }
        public string GetOptionDVTByMaMH(string maMatHang)
        {
            string result = string.Empty;
            string optionDVT = string.Empty;
            optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            List<SubCategoryResult> lstDVT = SubCategoryUtil.GetDonViTinhByMatHang(maMatHang);
            if (lstDVT != null)
            {
                foreach (SubCategoryResult objDVT in lstDVT)
                {
                    optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                }

                result = @"<select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange=CheckSoLuongTonMatHang('" + maMatHang + "',this.value) id='drlDonViTinh_" + maMatHang + @"'  style='width:98%'>  " + optionDVT + @"                             
                            </select>";

            }
            return result;
        }
        public string GetOptionDVTByMaMH(string maMatHang, string defaultValue)
        {
            string result = string.Empty;
            string optionDVT = string.Empty;
            optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
            List<SubCategoryResult> lstDVT = SubCategoryUtil.GetDonViTinhByMatHang(maMatHang);
            if (lstDVT != null)
            {
                foreach (SubCategoryResult objDVT in lstDVT)
                {
                    if (objDVT.SubCategoryId.ToString() == defaultValue)
                        optionDVT += "<option  selected='selected' value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                    else optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                }

                result = @"<select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange=CheckSoLuongTonMatHang('" + maMatHang + "',this.value) id='drlDonViTinh_" + maMatHang + @"'  style='width:98%'>  " + optionDVT + @"                             
                            </select>";

            }
            return result;
        }       
        [Ajax.AjaxMethod()]
        public string GetSoLuongTon(string maMH, string makho, string maDVT)
        {
            int result = MatHangUtil.GetSoLuongTonByDonViTinh(maMH, ConvertValueObject.ParseInt(makho), ConvertValueObject.ParseInt(maDVT));
            return result.ToString();
        }
        [Ajax.AjaxMethod()]
        public string InsertData(string command, string maphieu, string khoFrom, string khoTo, string chungXe, string nguoinhap, string ngaynhap, string data)
        {
            DateTime now = CommonFunction.Now;
            string[] arrRow = data.Split('*');
            if (arrRow.Length > 0)
            {
                List<ChiTietPhieuChuyenKho> lstChiTiet = new List<ChiTietPhieuChuyenKho>();
                for (int i = 0; i < arrRow.Length - 1; i++)
                {
                    lstChiTiet.Add(GetRowData(arrRow[i].ToString()));
                }
                PhieuChuyenKho objPhieuChuyen = new PhieuChuyenKho();
                objPhieuChuyen.MaKhoChuyen = ConvertValueObject.ParseInt(khoFrom);
                objPhieuChuyen.MaKhoDen = ConvertValueObject.ParseInt(khoTo);
                objPhieuChuyen.ChungXe = ConvertValueObject.ParseInt(chungXe);
                objPhieuChuyen.NgayNhap =CommonFunction.SetTime(ConvertValueObject.ParseDateTime(ngaynhap),now.Hour,now.Minute,now.Second);
                objPhieuChuyen.NguoiNhap = nguoinhap;
                objPhieuChuyen.SysCreatedBy = nguoinhap;
                objPhieuChuyen.SysCreatedDate = now;
                objPhieuChuyen.SysUpdatedBy = nguoinhap;
                objPhieuChuyen.SysUpdatedDate = now;
                if (command == CommonFunction.DEFAULT_COMMAND_EDIT)
                {
                    Int64 maPhieuChuyen = ConvertValueObject.ParseLong(maphieu);
                    if (maPhieuChuyen > 0)
                    {
                        objPhieuChuyen.MaPhieuChuyenKho = maPhieuChuyen;
                        if (PhieuChuyenKhoUtil.Update(objPhieuChuyen, lstChiTiet))
                            return maPhieuChuyen.ToString();
                        return "0";

                    }
                    else return "0";
                }
                else
                {
                    Int64 id = PhieuChuyenKhoUtil.Insert(objPhieuChuyen, lstChiTiet);
                    if (id > 0)
                        return id.ToString();
                    return "0";
                }
            }
            return "0";
        }
        private ChiTietPhieuChuyenKho GetRowData(string data)
        {
            string[] arr = data.Split(';');
            if (arr.Length > 0)
            {
                ChiTietPhieuChuyenKho objData = new ChiTietPhieuChuyenKho();
                objData.MaMatHang = arr[0].ToString();
                objData.SoLuongChuyen = ConvertValueObject.ParseInt(arr[1].ToString());
                objData.MaDonViTinh = ConvertValueObject.ParseInt(arr[2].ToString());
                return objData;
            }
            return null;
        }
        #endregion

    }
}
